﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;

namespace BackupMirthToTFS
{
    public static class Email
    {
        public static void SendAlertEmail(string sendTo, string subject, string emailBody, MailPriority mailPriority = MailPriority.Normal)
        {
            MailMessage message = BuildMailMessage(sendTo, subject, emailBody, mailPriority);             
            SendEmailViaSmtp(message);
        }

        private static MailMessage BuildMailMessage(string sendTo, string subject, string emailBody, MailPriority mailPriority)
        {
            var message = new MailMessage
            {
                Subject = subject,
                Body = emailBody,
                Priority = mailPriority,
                From = new MailAddress(Config.NotificationEmailAddress)
            };

            foreach (var emailAddr in sendTo.Split(';'))
            {
                message.To.Add(emailAddr);
            }

            return message;
        }

        private static void SendEmailViaSmtp(MailMessage emailMessage)
        {
            using (var smtp = new SmtpClient(Config.SmtpServer))
            {
                smtp.Send(emailMessage);
            }
        }
    }
}